import { BaseApi, ClientOptions } from "./flora-api-base";
import cn from './locales/cn';
import en from './locales/en';

export function createTr(opts: { clientOptions: ClientOptions, base: BaseApi }) {
  const ff = (key: string, args: string[]) => {
    return key.replace(/{(\d+)}/g, function (match, number) {
      return typeof args[number] != 'undefined'
        ? args[number]
        : match
        ;
    });
  }

  const tr = (key: string, args?: string[]) => {
    let str = ''
    switch (opts.base.lang) {
      case 'en':
        str = en[key] || key
        break;
      default:
        str = cn[key] || key
        break;
    }
    if (args?.length) {
      str = ff(str, args)
    }
    return str
  }

  return tr;
};
